import com.mysql.jdbc.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TransactionExample {

    public static void main(String[] args) {
        String jdbcUrl = "jdbc:mysql://localhost:3306/spring6";
        String username = "root";
        String password = "123456";
        Connection connection = null;
        try {
            connection = (Connection) DriverManager.getConnection(jdbcUrl, username, password);
            connection.setAutoCommit(false);
            Statement statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO t_account(id, balance, accountId ) VALUES (12, 100,10086)");
            int i=1/0;
            statement.executeUpdate("UPDATE t_account SET balance = 200  WHERE id=12");
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (connection != null) {
                    connection.rollback();
                }
            } catch (SQLException rollbackException) {
                rollbackException.printStackTrace();
            }
        } finally {
            try {
                if (connection != null) {
                    connection.setAutoCommit(true);
                    connection.close();
                }
            } catch (SQLException closeException) {
                closeException.printStackTrace();
            }
        }
    }
}